<template>
  <div class="banner-list-mian">
    <Sticky :class-name="'sub-navbar'">
      <div class="web-title">banner列表</div>
    </Sticky>
    <div class="app-container">
      <div class="filter-container">
        <el-select
          v-model="listQuery.client_side"
          placeholder="选择平台"
          clearable
          class="filter-item"
          @change="handleList"
          @clear="handleList"
        >
          <el-option :value="1">web</el-option>
          <el-option :value="2">shop</el-option>
          <el-option :value="3">手机</el-option>
        </el-select>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleList"
        >查询</el-button>
      </div>
      <el-table
        :key="tabKey"
        :data="list"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          label="ID"
          align="center"
          prop="id"
          width="80"
        />
        <el-table-column
          label="名称"
          align="center"
          prop="title"
          width="100"
        />
        <el-table-column
          label="链接"
          align="center"
          prop="link"
          width="300"
        />
        <el-table-column label="展示平台" align="center" width="80">
          <template slot-scope="{ row }">
            <span>{{ clientSide[row.client_side*1 - 1] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" width="100">
          <template slot-scope="{ row }">
            <el-input v-model="row.sort" placeholder="请输入排序" type="number" @change="changeSort(row)" />
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="create_time"
          align="center"
          width="150"
        />
        <el-table-column
          label="更新时间"
          prop="update_time"
          align="center"
          width="150"
        />
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button v-if="row.status===1" type="success" size="mini" @click="changeShow(row)">显示</el-button>
            <el-button v-else type="danger" size="mini" @click="changeShow(row)">禁止</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button type="primary" icon="el-icon-edit" size="mini" plain @click="editBanner(row.id)">修改</el-button>
            <el-button v-waves type="danger" size="mini" icon="el-icon-delete" plain @click="removeConfirm(row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="pageInfo.total * pageInfo.pageSize"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.pageSize"
        @pagination="handleList()"
      />
    </div>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky/index'
import { getList, changeFiled, remove } from '../../api/banner'
import Pagination from '@/components/Pagination/index'
import waves from '@/directive/waves/waves'
import { removeSingleImage } from '@/api/upload-image'
export default {
  components: { Pagination, Sticky },
  directives: { waves },
  data() {
    return {
      tabKey: 1,
      list: [],
      listQuery: {},
      pageInfo: {
        page: 1,
        pageSize: 2,
        total: 0
      },
      clientSide: ['网站', '商城', '手机']
    }
  },
  mounted() {
    this.handleList()
  },
  methods: {
    handleList() {
      if (!this.listQuery.client_side && this.listQuery.client_sidev === '') {
        this.listQuery = {}
      }
      const postForm = {
        pageInfo: this.pageInfo,
        listQuery: this.listQuery
      }
      getList(postForm).then((responst) => {
        const { pageInfo, list } = responst.data
        this.list = list
        this.pageInfo = pageInfo
      })
    },
    changeSort(item) {
      if (item.sort === '') {
        this.$message({
          message: '排序不能为空',
          type: 'error'
        })
        return
      }
      const postData = {
        'id': item.id,
        'postForm': {
          'sort': item.sort
        }
      }
      changeFiled(postData).then((response) => {
        this.$message({
          message: '排序更改成功',
          type: 'success'
        })
      })
    },
    editBanner(id) {
      // to do
      this.$router.push('/banner/edit/' + id)
    },
    removeItem(item) {
      const postData = {
        'id': item.id
      }
      remove(postData).then((response) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        const postDataUrl = {
          'file': item.banner_url
        }
        removeSingleImage(postDataUrl).then(responses => {
          console.log(responses)
        })
        this.handleList()
      })
    },
    removeConfirm(item) {
      this.$confirm('此操作将永久的删除该调信息，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.removeItem(item)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    changeShow(item) {
      item.status = 1 - item.status
      const postData = {
        'id': item.id,
        'postForm': {
          'status': item.status
        }
      }
      changeFiled(postData).then((response) => {
        this.$message({
          message: '状态更改成功',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.banner-list-mian{
  .filter-container{
    .filter-item{
      margin-right: 15px;
    }
  }
}
</style>
